/**
 * 
 */
package com.shiplanet.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.shiplanet.dao.PostDAO;
import com.shiplanet.dao.impl.BaseDAOImpl;
import com.shiplanet.entity.Category;
import com.shiplanet.entity.Post;

/**
 * @author joesong
 *
 */
@Repository("postDAO")
public class PostDAOImpl extends BaseDAOImpl<Post> implements PostDAO {

	@SuppressWarnings("unchecked")
	public List<Post> findPostsByCategoryAndKeyword(
			List<Category> catList, String keyword) {
		Query q;
		if (catList == null) {
			q = getCurrentSession()
					.createQuery(
							"from Post as p where p.equipmentName like :equipmentName")
					.setParameter("equipmentName", "%" + keyword + "%");
		} else {
			q = getCurrentSession()
					.createQuery(
							"from Post as p where p.category in (:catList) and p.equipmentName like :equipmentName")
					.setParameterList("catList", catList)
					.setParameter("equipmentName", "%" + keyword + "%");
		}

		return q.list();
	}

	@SuppressWarnings("unchecked")
	public List<Post> findPostsByCategory(List<Category> catList) {
		Query q = getCurrentSession().createQuery(
				"from Post as p where p.category in (:catList)")
				.setParameterList("catList", catList);

		return q.list();
	}
	
	public long countPost() {
		return count("select count(*) from Post");
	}

	public List<Post> findPost(int page, int rows) {
		return find("from Post", new Object[0], page, rows);
	}

	public List<Post> findFeaturedPost() {
		return find("from Post as p where p.isFeatured = true");
	}	
	
	
}
